#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using Neatly.UI;
using UnityEditor;
using UnityEditor.AnimatedValues;
using UnityEngine;
using UnityEngine.UI;

namespace NeatlyEditor.UI
{
    [CustomEditor(typeof(NButtonBase), true)]
    [CanEditMultipleObjects]
    public class NButtonBaseEditor : Editor
    {
        #region SerializedProperty
        SerializedProperty m_TransitionProperty;
        SerializedProperty m_TargetGraphicProperty;
        SerializedProperty m_InteractableProperty;
        SerializedProperty m_ColorBlockProperty;
        SerializedProperty m_PressedSpriteNameProperty;
        SerializedProperty m_DisableSpriteNameProperty;
        SerializedProperty m_OffGraphicsProperty;
        SerializedProperty m_ScaleTweenProperty;
        SerializedProperty m_SoundEnableProperty;
        SerializedProperty m_SoundNameProperty;
        SerializedProperty m_VirtualKeyCodeProperty;
        #endregion

        #region GUIContent
        GUIContent m_OffGraphicsContent;
        GUIContent m_SoundNameContent;
        #endregion

        readonly AnimBool m_ShowSpriteTint = new AnimBool();
        readonly AnimBool m_ShowColorTint = new AnimBool();
        readonly AnimBool m_ShowSoundName = new AnimBool();

        protected virtual void OnEnable()
        {
            m_InteractableProperty = serializedObject.FindProperty("m_Interactable");
            m_TargetGraphicProperty = serializedObject.FindProperty("m_TargetGraphic");
            m_TransitionProperty = serializedObject.FindProperty("m_Transition");
            m_ColorBlockProperty = serializedObject.FindProperty("m_Colors");
            m_PressedSpriteNameProperty = serializedObject.FindProperty("pressedSpriteName");
            m_DisableSpriteNameProperty = serializedObject.FindProperty("disableSpriteName");
            m_OffGraphicsProperty = serializedObject.FindProperty("m_OffGraphics");
            m_ScaleTweenProperty = serializedObject.FindProperty("m_ScaleTween");
            m_SoundEnableProperty = serializedObject.FindProperty("m_SoundEnable");
            m_SoundNameProperty = serializedObject.FindProperty("m_SoundName");
            m_VirtualKeyCodeProperty = serializedObject.FindProperty("virtualKeyCode");


            m_OffGraphicsContent = new GUIContent("控制Graphic");
            m_SoundNameContent = new GUIContent("音效名");

            var trans = GetTransition(m_TransitionProperty);
            m_ShowColorTint.value = (trans == NButtonBase.Transition.ColorTint);
            m_ShowSpriteTint.value = (trans == NButtonBase.Transition.Sprite);
            m_ShowSoundName.value = m_SoundEnableProperty.boolValue;
            m_ShowColorTint.valueChanged.AddListener(Repaint);
            m_ShowSpriteTint.valueChanged.AddListener(Repaint);
            m_ShowSoundName.valueChanged.AddListener(Repaint);
        }

        protected virtual void OnDisable()
        {
            m_ShowColorTint.valueChanged.RemoveListener(Repaint);
            m_ShowSpriteTint.valueChanged.RemoveListener(Repaint);
            m_ShowSoundName.valueChanged.RemoveListener(Repaint);
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            EditorGUILayout.PropertyField(m_InteractableProperty);
            var trans = GetTransition(m_TransitionProperty);
            var graphic = m_TargetGraphicProperty.objectReferenceValue as Graphic;
            if (graphic == null)
                graphic = ((NButtonBase)target).GetComponent<Graphic>();
            m_ShowColorTint.target = (!m_TransitionProperty.hasMultipleDifferentValues && trans == NButtonBase.Transition.ColorTint);
            m_ShowSpriteTint.target = (!m_TransitionProperty.hasMultipleDifferentValues && trans == NButtonBase.Transition.Sprite);
            EditorGUILayout.PropertyField(m_TransitionProperty);
            ++EditorGUI.indentLevel;
            {
                if (trans == NButtonBase.Transition.ColorTint||trans == NButtonBase.Transition.Sprite)
                {
                    EditorGUILayout.PropertyField(m_TargetGraphicProperty);
                }

                switch (trans)
                {
                    case NButtonBase.Transition.ColorTint:
                        if (graphic == null)
                            EditorGUILayout.HelpBox("You must have a Graphic target in order to use a color transition.", MessageType.Warning);
                        break;
                    case NButtonBase.Transition.Sprite:
                        if (graphic == null)
                            EditorGUILayout.HelpBox("You must have a Graphic target in order to use a sprite transition.", MessageType.Warning);
                        break;
                }

                if (EditorGUILayout.BeginFadeGroup(m_ShowColorTint.faded))
                {
                    EditorGUILayout.PropertyField(m_ColorBlockProperty);
                }
                EditorGUILayout.EndFadeGroup();
                
                if (EditorGUILayout.BeginFadeGroup(m_ShowSpriteTint.faded))
                {
                    EditorGUILayout.PropertyField(m_PressedSpriteNameProperty);
                    EditorGUILayout.PropertyField(m_DisableSpriteNameProperty);
                }
                EditorGUILayout.EndFadeGroup();
            }
            --EditorGUI.indentLevel;
            EditorGUILayout.PropertyField(m_OffGraphicsProperty, m_OffGraphicsContent, true);
            EditorGUILayout.PropertyField(m_ScaleTweenProperty);
            EditorGUILayout.PropertyField(m_SoundEnableProperty);
            m_ShowSoundName.target = m_SoundEnableProperty.boolValue;
            ++EditorGUI.indentLevel;
            if (EditorGUILayout.BeginFadeGroup(m_ShowSoundName.faded))
            {
                EditorGUILayout.PropertyField(m_SoundNameProperty, m_SoundNameContent);
                if (string.IsNullOrEmpty(m_SoundNameProperty.stringValue))
                    EditorGUILayout.HelpBox("当前为默认音效", MessageType.Info);
            }
            EditorGUILayout.EndFadeGroup();
            --EditorGUI.indentLevel;
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(m_VirtualKeyCodeProperty);

            serializedObject.ApplyModifiedProperties();
        }


        static NButtonBase.Transition GetTransition(SerializedProperty transition)
        {
            return (NButtonBase.Transition)transition.enumValueIndex;
        }
    }
}
